package com.innovation.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class PatUtil {
	
	/**
	 * GZIP压缩字符串
	 * @param content
	 * @return
	 * @throws IOException
	 */
	public static byte[] compress(String content) throws IOException{
		if(content == null){
			return null;
		}
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		GZIPOutputStream gos = new GZIPOutputStream(buf);
		byte[] data = content.getBytes("UTF-8");
		gos.write(data);
		gos.finish();
		gos.flush();
		gos.close();
		
		return buf.toByteArray();
	}
	
	/**
	 * 解压缩
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public static byte[] decompress(byte[] data) throws IOException{
		if(data == null || data.length == 0){
			return null;
		}
		ByteArrayInputStream bis = new ByteArrayInputStream(data);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		GZIPInputStream gis = new GZIPInputStream(bis);

		int count;
		byte temp[] = new byte[1024];
		while ((count = gis.read(temp, 0, 1024)) != -1) {
			bos.write(temp, 0, count);
		}
		gis.close();
		data = bos.toByteArray();
		bos.flush();
		bos.close();
		bis.close();
		return data;
	}
	
	public static byte[] httpPost(String url, String data) throws Exception{
		byte[] res = null;
		URL postUrl = new URL(url);
		// 打开连接
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        // 设置是否向connection输出，因为这个是post请求，参数要放在
        // http正文内，因此需要设为true
        connection.setDoOutput(true);
        // Read from the connection. Default is true.
        connection.setDoInput(true);
        // Set the post method. Default is GET
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        // 配置本次连接的Content-type，配置为application/x-www-form-urlencoded的
        // 意思是正文是urlencoded编码过的form参数，下面我们可以看到我们对正文内容使用URLEncoder.encode
        // 进行编码
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 连接，从postUrl.openConnection()至此的配置必须要在connect之前完成，
        // 要注意的是connection.getOutputStream会隐含的进行connect。
        connection.connect();
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        // 正文，正文内容其实跟get的URL中'?'后的参数字符串一致
        String content = "cmd=" + URLEncoder.encode(data, "utf-8");
        // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
        out.writeBytes(content); 
        out.flush();
        out.close(); // flush and close
        
	     //获得响应状态
	     int responseCode = connection.getResponseCode();
	     if (HttpURLConnection.HTTP_OK == responseCode) {
	     	byte[] buffer = new byte[4096];
	     	int len = -1;
	     	InputStream is = connection.getInputStream();
	     	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	     	while ((len = is.read(buffer)) != -1) {
	     		bos.write(buffer, 0, len);
	     	}
	     	res = bos.toByteArray();
	     	is.close();
	     	String response = new String(res, "UTF-8");
	     	System.out.println("response message:" + response + "(" +url + " Response Code:" + responseCode+")");
	     } else {
	     	System.out.println(url + " Response Code:" + responseCode);
	     }
		return res;
	}
	
	public static void main(String[] args) throws IOException {
		String content = "{\"dst_id\": 10000,\"dst_name\": \"张福生卡斯柯对方加\",\"extra_data\": \"p\uff83\uffcc\ufffe\uff8e\u0005\"}";
		byte[] bytes = content.getBytes("UTF-8");
		byte[] bytes1 = compress(content);
		byte[] bytes2 = decompress(bytes1);
		
		System.out.println(bytes.length+" "+bytes1.length + " "+ bytes2.length);
		System.out.println(new String(bytes2, "UTF-8"));
	}
}
